package org.jeecg.modules.online.cgform.service.impl;

import java.util.List;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.DataBaseConstant;
import org.jeecg.modules.online.cgform.entity.OnlCgformIndex;
import org.jeecg.modules.online.cgform.mapper.OnlCgformHeadMapper;
import org.jeecg.modules.online.cgform.mapper.OnlCgformIndexMapper;
import org.jeecg.modules.online.cgform.service.IOnlCgformIndexService;
import org.jeecg.modules.online.config.util.DbTableUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: Online表单开发
 * @Author: jeecg-boot
 * @Date:   2019-03-12
 * @Version: V1.0
 */
@Slf4j
@Service("onlCgformIndexServiceImpl")
public class OnlCgformIndexServiceImpl extends ServiceImpl<OnlCgformIndexMapper, OnlCgformIndex> implements IOnlCgformIndexService {

	@Autowired
	private OnlCgformHeadMapper onlCgformHeadMapper;
	
	
	@Override
	public void createIndex(String code,String databaseType,String tbname) {
		// TODO Auto-generated method stub
		LambdaQueryWrapper<OnlCgformIndex> query = new LambdaQueryWrapper<OnlCgformIndex>();
		query.eq(OnlCgformIndex::getCgformHeadId, code);
		List<OnlCgformIndex> indexes = this.list(query);
		if(indexes!=null && indexes.size()>0){
			for(OnlCgformIndex cgformIndex : indexes){
				if(!CommonConstant.DEL_FLAG_1.equals(cgformIndex.getDelFlag()) && "N".equals(cgformIndex.getIsDbSynch())){
					String sql = "";
					String indexName = cgformIndex.getIndexName();
					String indexField = cgformIndex.getIndexField();
					String indexType = "normal".equals(cgformIndex.getIndexType())?" index ":cgformIndex.getIndexType()+ " index ";
					//TODO 确认创建索引的语句是否是一样的,如果一样 没必要判断了
					switch (databaseType) {
						case DataBaseConstant.DB_TYPE_MYSQL:
							sql = "create " + indexType  + indexName + " on " + tbname + "(" + indexField + ")";
							break;
						case DataBaseConstant.DB_TYPE_ORACLE:
							sql = "create " + indexType  + indexName + " on " + tbname + "(" + indexField + ")";
							break;
						case DataBaseConstant.DB_TYPE_SQLSERVER:
							sql = "create " + indexType + indexName + " on " + tbname + "(" + indexField + ")";
							break;
						case DataBaseConstant.DB_TYPE_POSTGRESQL:
							sql = "create " + indexType + indexName + " on " + tbname + "(" + indexField + ")";
							break;
						default:
							sql = "create " + indexType + indexName + " on " + tbname + "(" + indexField + ")";
							break;
					}
					log.info(" 创建索引 executeDDL ："+sql);
					this.onlCgformHeadMapper.executeDDL(sql);
					//修改索引配置为已同步
					cgformIndex.setIsDbSynch("Y");
					this.updateById(cgformIndex);
				}
			}
		}
	}


	@Override
	public boolean isExistIndex(String countSql) {
		if(countSql==null) {
			return true;
		}
		Integer count = baseMapper.queryIndexCount(countSql);
		if(count!=null && count>0) {
			return true;
		}
		return false;
	}

	@Override
	public List<OnlCgformIndex> getCgformIndexsByCgformId(String cgformId) {
		return baseMapper.selectList(new LambdaQueryWrapper<OnlCgformIndex>().in(OnlCgformIndex::getCgformHeadId, cgformId));
	}

}
